package org.teachertools.web.actions;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.teachertools.data.Competence;
import org.teachertools.services.CompetenceService;
import org.teachertools.web.forms.CompetenceForm;
import org.teachertools.web.forms.CompetenceFormMapping;

public class CompetenceAction extends CommonDispatchAction {
    @Override
    protected Map<String, String> getKeyMethodMap() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("button.save", "save");
        map.put("button.delete", "delete");
        map.put("button.search", "search");
        return map;
    }

    public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        CompetenceForm competenceForm = (CompetenceForm) form;
        boolean isUpdate = competenceForm.getIsUpdate().booleanValue();
        CompetenceService competenceService = this.getServiceLocator().getCompetenceService();
        if (isUpdate) {
            Competence loadedCompetence = competenceService.get(competenceForm.getId());
            Competence competence = CompetenceFormMapping.mappingFormToDTO(competenceForm, loadedCompetence);
            competenceService.update(competence);
        } else {
            Competence competence = CompetenceFormMapping.mappingFormToDTO(competenceForm);
            competenceService.save(competence);
        }

        ActionMessages actionMessages = new ActionMessages();
        actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("success.do", getMessage(request,
                "libelle.lenregistrement")));
        saveMessages(request, actionMessages);

        competenceForm.setId(null);
        competenceForm.setNom(null);

        addCategoryList(request);
        Long idCategorie = competenceForm.getIdCategorie();
        request.setAttribute("idCategorie", idCategorie);
        addMatiereList(request);
        return mapping.findForward("successSave");
    }

    public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        CompetenceForm competenceForm = (CompetenceForm) form;

        CompetenceService competenceService = this.getServiceLocator().getCompetenceService();
        Long id = competenceForm.getId();
        competenceService.deleteById(id);
        competenceForm.reset();

        ActionMessages actionMessages = new ActionMessages();
        actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("success.do", getMessage(request,
                "libelle.lasuppression")));
        saveMessages(request, actionMessages);

        addCategoryList(request);

        return mapping.findForward("successDelete");
    }

    public ActionForward search(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        CompetenceForm competenceForm = (CompetenceForm) form;

        Competence competence = CompetenceFormMapping.mappingFormToDTO(competenceForm);
        CompetenceService competenceService = getServiceLocator().getCompetenceService();
        Collection<Competence> list = competenceService.searchByExample(competence);

        request.setAttribute("RESULTS_LIST", list);

        addCategoryList(request);
        addMatiereList(request);

        return mapping.findForward("successSearch");
    }
}
